package com.software.frontend.mine.adapters;

import static ly.img.android.pesdk.backend.decoder.ImageSource.getResources;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.software.frontend.BuildConfig;
import com.software.frontend.R;
import com.software.frontend.mine.entity.Comment;
import com.software.frontend.mine.entity.Fans;
import com.software.frontend.mine.entity.Like;
import com.software.frontend.mine.entity.SystemNotice;
import com.software.frontend.mine.utils.PictureProcessUtils;

import java.util.List;

public class FansAdapter extends BaseAdapter {

    LayoutInflater inflater;
    List<Fans> data;

    public FansAdapter(Context context, List data) {
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Fans fans = data.get(i);
        if (view == null) {
            view = (ConstraintLayout)inflater.inflate(R.layout.item_notices_fans, viewGroup,false);
        }
        //需要判断传入的Fans是否合法
        if (isFansLegal(fans)) {
            TextView timeLate = (TextView) view.findViewById(R.id.textView8);
            if (fans.getTimeLate() == 0){
                timeLate.setText("刚刚");
            }else {
                timeLate.setText(fans.getTimeLate() + "分钟前");
            }
            ImageView HandlerHead = (ImageView)view.findViewById(R.id.imageView);
            HandlerHead.setImageBitmap(PictureProcessUtils.toRoundBitmap(fans.getHead()));
            Button button = (Button) view.findViewById(R.id.button);
            if(fans.isHasReAction()){
                button.setText("已回关");
            }else {
                button.setText("关注");
            }
            //点击回关，并调用后端接口实现数据持久化
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (button.getText().toString().equals("关注")){
                        button.setText("已回关");
                    }
                }
            });
            TextView name = (TextView)view.findViewById(R.id.textView7);
            name.setText(fans.getName());
        }else {
            errorHandle();
        }

        return view;
    }

    private boolean isFansLegal(Fans fans) {
        if (fans.getName() != null && !fans.getName().equals("")
                && fans.getTimeLate() >= 0
                && fans.getHead() != null){
            return true;
        }else {
            return false;
        }

    }

    private void errorHandle() {
        //错误处理
    }
}
